﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseAccess;
using Liguo.AppCode;
using System.Data;

namespace Liguo.Store
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //如已登录则清空session，防止用户在同一个会话中登录两次
            if (Session["User"] != null)
            {
                Session.Clear();
            }
        }

        protected void BtLogin_Click(object sender, EventArgs e)
        {
            #region 用户名检查
            if (TbUserName.Text.Length == 0)
            {
                Response.Write("<script>alert('用户名不能为空！');</script>");
                return;
            }

            if (TbUserName.Text.Length > 20)
            {
                Response.Write("<script>alert('用户名过长！');</script>");
                TbUserName.Text = "";
                return;
            }

            if (!Utilities.IsCJKChar(TbUserName.Text))
            {
                Response.Write("<script>alert('用户名含非法字符！');</script>");
                TbUserName.Text = "";
                return;
            }
            #endregion

            String UserName = TbUserName.Text;

            #region 密码检查
            if (TbPassword.Text.Length == 0)
            {
                Response.Write("<script>alert('密码不能为空！');</script>");
                return;
            }
            #endregion

            String Password = TbPassword.Text;

            int erro = LoginAction(UserName, Password);
            if (erro == 0)
            {
                //Response.Write("<script>alert('登录成功！');</script>");
                Response.Write("<script>top.location='Store.aspx';</script>");
            }
            else if (erro == 1)
            {
                Response.Write("<script>alert('用户不存在！');</script>");
                TbPassword.Text = "";
                return;
            }
            else if (erro == 2)
            {
                Response.Write("<script>alert('密码错误！');</script>");
                TbPassword.Text = "";
                return;
            }
            else if (erro == 3)
            {
                Response.Write("<script>alert('您的账号已经被封！');</script>");
                Response.Write("<script>top.location=\"Banned.aspx\";</script>");
            }
            else if (erro == 4)
            {
                Response.Write("<script>alert('您的账号尚未激活，请前往激活！');</script>");
                Response.Write("<script>top.location=\"Registed.aspx\";</script>");
            }
        }

        private int LoginAction(String UserName, String Password)
        {
            User user = GetUserInfo(UserName);
            if (user == null)
            {
                //用户不存在
                return 1;
            }
            else
            {
                String pwdplus = Utilities.GetMD5(Password + user.Salt);
                if (!pwdplus.Equals(user.Password))
                {
                    //密码不匹配
                    return 2;
                }
                else
                {


                    if (user.Status == 0)
                    {
                        //账号被封
                        return 3;
                    }
                    else if (user.Status == 2)
                    {
                        //账号未激活
                        Session.Add("UserID", user.UserID);
                        Session.Add("UserName", user.UserName);
                        Session.Add("MailBox", user.MailBox);
                        Session.Timeout = 30;
                        return 4;
                    }
                    else
                    {
                        //向session中添加用户信息
                        Session.Add("User", user);
                        Session.Timeout = 30;
                        return 0;
                    }
                }
            }
        }

        private User GetUserInfo(String UserName)
        {
            SqlIt sql = new SqlIt();
            if (!sql.IsExist("UserInfo", "UserName='" + UserName + "'"))
            {
                return null;
            }
            else
            {
                User user = new User();
                DataSet ds = sql.SpecialItem("UserInfo", "UserName='" + UserName + "'");
                user.UserID = (Int32)ds.Tables[0].Rows[0]["ID"];
                user.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                user.MailBox = ds.Tables[0].Rows[0]["MailBox"].ToString();
                user.Password = ds.Tables[0].Rows[0]["Password"].ToString();
                user.Salt = ds.Tables[0].Rows[0]["Salt"].ToString();
                user.Status = (Int32)ds.Tables[0].Rows[0]["Status"];
                user.Score = (Int32)ds.Tables[0].Rows[0]["Score"];
                user.Reputation = (Int32)ds.Tables[0].Rows[0]["Reputation"];
                user.RegTime = Convert.ToDateTime(ds.Tables[0].Rows[0]["RegTime"]);
                if (ds.Tables[0].Rows[0]["DefDelivery"] == DBNull.Value)
                {
                    user.DefaultDelivery = -1;
                }
                else
                {
                    user.DefaultDelivery = (Int32)ds.Tables[0].Rows[0]["DefDelivery"];
                }
                return user;
            }
        }
    }
}